package com.qcby.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.community.annotation.LogAnnotation;
import com.qcby.community.entity.*;
import com.qcby.community.form.CommunityAddOrUpdateForm;
import com.qcby.community.form.CommunityListForm;
import com.qcby.community.mapper.CommunityMapper;
import com.qcby.community.service.*;
import com.qcby.community.util.Result;
import com.qcby.community.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/community")
public class CommunityController {

    @Autowired
    private CommunityService communityService;

    @Autowired
    private CameraService cameraService;

    @Autowired
    private InOutRecordService inOutRecordService;

    @Autowired
    private PersonService personService;

    @Autowired
    private ManualRecordService manualRecordService;

    /**
     * 小区管理的分页条件查询
     * @param communityListForm
     * @return
     */
    @GetMapping("/list")
    public Result pageList(CommunityListForm communityListForm){
        //实现小区分页条件查询，按照PageVO的形式返回结果
        PageVO pageVO = this.communityService.communityList(communityListForm);
        return Result.ok().put("data",pageVO);
    }

    /**
     * 添加小区
     * @param communityAddOrUpdateForm
     * @param session
     * @return
     */
    @LogAnnotation("添加小区")
    @PostMapping("/add")
    public Result add(@RequestBody CommunityAddOrUpdateForm communityAddOrUpdateForm, HttpSession session){
        Community community = new Community();
        BeanUtils.copyProperties(communityAddOrUpdateForm, community);
        //将数据添加的操作人员进行记录
        User user = (User) session.getAttribute("user");
        //存入数据库的小区对象添加创建人
        community.setCreater(user.getUsername());
        //进行存储
        boolean save = this.communityService.save(community);
        if(!save){
            return Result.error("小区添加失败");
        }
        return Result.ok();
    }

    /**
     * 修改的回显
     * 通过id查询小区
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Integer id){
        Community community = this.communityService.getById(id);
        if(community == null){
            return Result.error("小区不存在");
        }
        return Result.ok().put("data",community);
    }

    /**
     * 修改小区
     * @param communityAddOrUpdateForm
     * @return
     */
    @LogAnnotation("编辑小区")
    @PutMapping("/edit")
    public Result edit(@RequestBody CommunityAddOrUpdateForm communityAddOrUpdateForm){
        Community community = new Community();
        BeanUtils.copyProperties(communityAddOrUpdateForm,community);
        boolean updateById = this.communityService.updateById(community);
        if(!updateById){
            return Result.error("编辑小区失败");
        }
        return Result.ok();
    }

    /**
     * 删除小区
     * @param ids
     * @return
     */
    @LogAnnotation("删除小区")
    @DeleteMapping("/del")
    @Transactional
    public Result del(@RequestBody Integer[] ids){
        try {
            //小区摄像头
            QueryWrapper<Camera> cameraQueryWrapper = new QueryWrapper<>();
            cameraQueryWrapper.in("community_id",ids);
            boolean remove1 = this.cameraService.remove(cameraQueryWrapper);
            //小区出入记录
            QueryWrapper<InOutRecord> inOutRecordQueryWrapper = new QueryWrapper<>();
            inOutRecordQueryWrapper.in("community_id",ids);
            boolean remove2 = this.inOutRecordService.remove(inOutRecordQueryWrapper);
            //小区访客记录
            QueryWrapper<ManualRecord> manualRecordQueryWrapper = new QueryWrapper<>();
            manualRecordQueryWrapper.in("community_id",ids);
            boolean remove3 = this.manualRecordService.remove(manualRecordQueryWrapper);
            //小区居民
            QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
            personQueryWrapper.in("community_id",ids);
            boolean remove4 = this.personService.remove(personQueryWrapper);
            //小区
            boolean remove5 = this.communityService.removeByIds(Arrays.asList(ids));
        } catch (Exception e) {
            return Result.error("小区删除失败");
        }

        return Result.ok();
    }

    /**
     * 小区地图
     * @return
     */
    @GetMapping("/getCommunityMap")
    public Result getCommunityMap(){
        List<Community> list = this.communityService.list();
        if(list == null){
            return Result.error("没有小区数据");
        }
        return Result.ok().put("data",list);
    }


}

